using UnityEngine;
using System.Collections;

public class TurnFade 
{
	private GameObject text;
	private float fadeAmount;
	private state actualState;
	private float colorI;
	
	public void initialize(state firstState,float amount,GameObject text)
	{
		fadeAmount = amount;
		this.text = text;
		
		Color color = text.renderer.material.color;
		
		if(firstState.Equals(state.FADEIN))
		{
			text.renderer.material.color = new Color(color.r,color.g,color.b,0);
			colorI = 0;
		}
		else
		{
			text.renderer.material.color = new Color(color.r,color.g,color.b,1);
			colorI = 1;
		}
		
		actualState = firstState;
	}
		
	public void execute()
	{
		if(actualState == state.FADEIN)
		{
			colorI += fadeAmount*Time.deltaTime;
			Color color = text.renderer.material.color;
			text.renderer.material.color = new Color(color.r,color.g,color.b,colorI);
			
			if(colorI >= 1)
			{
				changeState();
			}
		}
		
		else
		{
			colorI -= fadeAmount*Time.deltaTime;
			Color color = text.renderer.material.color;
			text.renderer.material.color = new Color(color.r,color.g,color.b,colorI);
			
			if(colorI <= 0)
			{
				changeState();
			}
		}
	}
			
	public void execute(float amount,GameObject text)
	{
		fadeAmount = amount;
		this.text = text;
		execute();	
	}
	
	public void changeState()
	{
		switch(actualState)
		{
			case state.FADEIN:	
				actualState = state.FADEOUT;
				break;
			
			case state.FADEOUT:
				actualState = state.FADEIN;
				break;
		}
	}
	
	public enum state
	{
		FADEIN,
		FADEOUT
	}
}
